﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWeb.Models;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;

namespace MvcWeb.Controllers
{
    [Export(typeof(IController)), ExportMetadata("Name", "User")]
    public class UserController : BaseController
    {

        [Import]
        public ISys_UserRepository uMod;


        [Import]
        public IDeptRepository deptModel;



        [Authorize]
        public ActionResult Index(string page)
        {
            var list = uMod.GetAllList();
            list = list.OrderByDescending(d => d.USERID);
            int size = 5;
            if (page != null)
            {
                ViewData["page"] = PagerHelper.Pager(Request.RawUrl.Substring(0, Request.RawUrl.IndexOf("?")), Convert.ToInt32(page), list.Count(), size, 4);
                list = list.Skip((Convert.ToInt32(Request["page"]) - 1) * size).Take(size);
            }
            else
            {
                ViewData["page"] = PagerHelper.Pager(Request.RawUrl, 1, list.Count(), size, 4);
                list = list.Skip((1 - 1) * size).Take(size);
            }
            return View(list);
        }

        [Authorize]
        public ActionResult Create()
        {

            ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName");
            return View();
        }

        //
        // POST: /User/Create

        [Authorize]
        [HttpPost]
        public ActionResult Create(string deptList, FormCollection collection)
        {
            try
            {
                SYS_USER mod = new SYS_USER();
                mod.USERID = DateTime.Now.Ticks.ToString();
                mod.ACCOUNT = Request.Form["Account"];
                mod.SEX = Convert.ToInt32(Request.Form["sex"]);
                mod.USERCNAME = Request.Form["UserCName"];
                mod.DEPARTID = deptList;
                mod.BIRTHDAY = Request.Form["BirthDay"];
                mod.TELEPHONE = Request.Form["Telephone"];
                mod.MOBILE = Request.Form["Mobile"];
                mod.EMAIL = Request.Form["Email"];
                mod.JOBNUMBER = Request.Form["JobNumber"];
                mod.ISENABLE = Request.Form["isEnable"] == "on" ? "1" : "0";
                mod.PASSWORD = "1";  //初始密码
                mod.CREATOR = LoginUser.ACCOUNT;
                mod.CREATETIME = DateTime.Now;

                uMod.Add(mod);
                uMod.UnitOfWork.SaveChanges();
                return RedirectToAction("Index");

            }
            catch (Exception ee)
            {
                ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName");
                return View();
            }
        }

        //
        // GET: /User/Edit/5

        [Authorize]
        public ActionResult Edit(string id)
        {
            SYS_USER mod = uMod.FirstOrDefault(itm=>itm.USERID==id);
            ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName");
            return View(mod);
        }

        //
        // POST: /User/Edit/5
        [Authorize]
        [HttpPost]
        public ActionResult Edit(string UserID, FormCollection collection)
        {
            try
            {
                SYS_USER mod = uMod.FirstOrDefault(itm => itm.USERID == UserID);
                mod.ACCOUNT = Request.Form["Account"];
                mod.SEX = Convert.ToInt32(Request.Form["sex"]);
                mod.USERCNAME = Request.Form["UserCName"];
                mod.DEPARTID = Request.Form["deptList"];
                mod.BIRTHDAY = Request.Form["BirthDay"];
                mod.TELEPHONE = Request.Form["Telephone"];
                mod.MOBILE = Request.Form["Mobile"];
                mod.EMAIL = Request.Form["Email"];
                mod.JOBNUMBER = Request.Form["JobNumber"];
                mod.ISENABLE = Request.Form["isEnable"] == "false" ? "0" : "1";

                uMod.UnitOfWork.SaveChanges();
                return RedirectToAction("Index");

            }
            catch (Exception ee)
            {
                ViewData["deptList"] = new SelectList(deptModel.GetAllList(), "deptID", "DeptCName");
                return View();
            }
        }


        //
        // POST: /User/Delete/5
        [Authorize]
        [HttpPost]
        public string Delete(string id)
        {


            try
            {
                // TODO: Add delete logic here
                uMod.Delete(itm => itm.USERID == id);
                uMod.UnitOfWork.SaveChanges();
                return "success";
            }
            catch
            {
                return "fail";
            }
        }
    }
}
